package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserDetailInfoBean;
import com.koron.css2.clientCenter.impl.UserInfoQuery;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.query.MsVerifyQueryBean;
import com.koron.css2.meterService.dto.MsVerifyDetailDto;
import com.koron.css2.meterService.dto.MsVerifyDto;
import com.koron.css2.meterService.mapper.MsVerifyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 校表查询
 * @author 周利斌
 *
 */
public class MsVeriQuery implements ServerInterface{

	private static Logger log = LogManager.getLogger(MsVeriQuery.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		MessageBean<Object> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
		try {
			MsVerifyQueryBean query = JsonUtils.objectToPojo(req.getData(), MsVerifyQueryBean.class);
			MsVerifyMapper mapper = factory.getMapper(MsVerifyMapper.class);
			UserInfoQuery u = new UserInfoQuery();
			//通过主键查询
			if(StringUtils.isNotBlank(query.getId())) {
				MsVerifyDetailDto msVerifyDetail = new MsVerifyDetailDto();
				MsVerifyDto msVerify = mapper.selectMsVerify(query.getId());
				UserDetailInfoBean userDetail = u.getUserDetailInfoByUserNo(factory, msVerify.getUserNo(),userInfo);
				msVerifyDetail.setUserDetailInfo(userDetail);
				msVerifyDetail.setMsVerify(msVerify);
				info.setData(msVerifyDetail);
				return info;
			}
			//模糊查询
			if(StringUtils.isNotBlank(query.getFuzzyQuery())) {
				UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
				List<MsVerifyDetailDto> data = new ArrayList<MsVerifyDetailDto>();
				List<UserBean> users = userInfoMapper.fuzzyQueryUserInfo(query.getFuzzyQuery(), 1);
				for(UserBean user : users) {
					MsVerifyDto msVerify = mapper.selectMsVerifyByMeterNo(user.getMeterNo());
					if(msVerify != null) {
						MsVerifyDetailDto msVerifyDetail = new MsVerifyDetailDto();
						UserDetailInfoBean userDetail = u.getUserDetailInfoByUser(factory, user,userInfo);
						msVerifyDetail.setUserDetailInfo(userDetail);
						msVerifyDetail.setMsVerify(msVerify);
						data.add(msVerifyDetail);
					}
				}
				info.setData(data);
				return info;
			}
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
